#!/bin/env python
#Date : 2014-08-26
#Author: sixther.me
#comment: check port status on linux server.usage:   ./check_port_status.py 80:httpd  3306:mysql 22:sshd
import socket,sys,os

del sys.argv[0]
down_process=[]
ip_list=[]
get_iplist_cmd="/sbin/ifconfig | grep 'inet addr' | awk '{print $2}' | awk -F: '{print $2}'"
for i in os.popen(get_iplist_cmd):
                ip_list.append(i.strip())
def check_port(addr,port):
        sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result=sock.connect_ex((addr,port))
        sock.close()
        if result == 0:
                return 0
        else:
                return 1
if __name__ ==  '__main__':
        for i in sys.argv:
                for j in ip_list:
                        return_num=check_port(j,int(i.split(":")[0]))
                        if return_num == 0:
                                code=''
                                break
                        else:
                                code='down'
                if code == 'down':
                        down_process.append(i.split(":")[1])
                code=''
        if len(down_process):
                print str(down_process) + " down!"
        else:
                print 0
